package org.dubnation.auth.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.dubnation.auth.domain.Permission;
import org.dubnation.auth.domain.RoleDTO;
import org.dubnation.auth.service.IRoleService;
import org.dubnation.auth.domain.Role;
import org.dubnation.auth.query.RoleQuery;
import org.dubnation.base.annotation.DubnationPermission;
import org.dubnation.base.util.AjaxResult;
import org.dubnation.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(value = "角色管理",description = "角色的CRUD")
@DubnationPermission(name = "角色管理")
public class RoleController {
    @Autowired
    public IRoleService roleService;

    /**
     * 保存和修改公用的
     * @param role  传递的实体
     * @return Ajaxresult转换结果
     */
    @ApiOperation(value = "新增/修改角色")
    @PutMapping
    @DubnationPermission(name = "新增/修改角色")
    public AjaxResult insertOrUpdate(@ApiParam(value = "角色",required = true) @RequestBody Role role){
        try {
            if( role.getId()!=null)
                roleService.update(role);
            else
                roleService.insert(role);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @ApiOperation(value = "角色数据删除")
    @DubnationPermission(name = "删除角色")
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@ApiParam(value = "角色id",required = true) @PathVariable("id") Long id){
        try {
            roleService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @ApiOperation(value = "根据id查询角色数据")
    @DubnationPermission(name = "根据id查询角色")
    @GetMapping("/{id}")
    public AjaxResult queryById(@ApiParam(value = "角色id",required = true) @PathVariable("id")Long id)
    {
        try {
            Role role = roleService.queryById(id);
            return AjaxResult.success(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @ApiOperation(value = "查询所有角色数据")
    @DubnationPermission(name = "查询所有角色")
    @GetMapping
    public AjaxResult queryAll(){

        try {
            List< Role> list = roleService.queryAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @ApiOperation(value = "分页查询角色数据")
    @DubnationPermission(name = "分页查询角色")
    @PostMapping
    public AjaxResult queryPage(@ApiParam(value = "角色关键字、当前页、每页条数",required = true) @RequestBody RoleQuery query)
    {
        try {
            PageInfo<Role> pageInfo = roleService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
    * 批量删除数据
    * @param ids
    * @return
    */
    @ApiOperation(value = "批量删除角色数据")
    @DubnationPermission(name = "批量删除角色")
    @PatchMapping
    public AjaxResult batchDelete(@ApiParam(value = "员工id数组",required = true) @RequestBody List<Long> ids)
    {
        try {
                roleService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除数据失败！"+e.getMessage());
        }
    }

    /**
     * DTO: data transfer object
     *      作用是当前端穿的数据和后端的domain差异较大的情况，
     *      就会封装一个与前端相近的对象
     * @param roleDTO
     * @return
     */
    @ApiOperation(value = "角色权限新增")
    @PutMapping("/permission")
    public AjaxResult insertRolePermission(@ApiParam(value = "权限和员工id",required = true) @RequestBody RoleDTO roleDTO)
    {
        try {
            roleService.insertRolePermission(roleDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("权限入库数据失败！"+e.getMessage());
        }
    }

    @ApiOperation(value = "根据角色id查询权限id")
    @GetMapping("/permission/{id}")
    public AjaxResult queryRolePermissionIds(@ApiParam(value = "角色id",required = true) @PathVariable("id") Long id)
    {
        try {
            List<Long> rolePermissionIds = roleService.queryRolePermissionIds(id);
            return AjaxResult.success(rolePermissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("权限入库数据失败！"+e.getMessage());
        }
    }

}
